<?php

namespace backend\models;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use common\models\DayTry;
/**
 * DayTrySearch represents the model behind the search form about `common\models\DayTry`.
 */
class DayTrySearch extends DayTry
{
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['day_try_id', 'product_id', 'status'], 'integer'],
            [['pic', 'recommend_time', 'add_user', 'add_time'], 'safe'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = DayTry::find();
        
        // add conditions that should always apply here

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
            'sort'=> ['defaultOrder' => ['day_try_id'=>SORT_DESC]] 
        ]);

        $this->load($params);

        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }

        // grid filtering conditions
        $query->andFilterWhere([
            'product_id' => $this->product_id,
            'status' => $this->status,
        ]);
        
        if(empty($params['end_time']))
        {
            $query->andFilterWhere(['>=', 'recommend_time', $this->recommend_time]);
        }elseif(empty($this->recommend_time))
        {
            $query->andFilterWhere(['<=', 'recommend_time',  $params['end_time']]);
        }else
        {
            $query->andFilterWhere(['between', 'recommend_time', $this->recommend_time, $params['end_time']]);
        }
//        $query->andFilterWhere(['like', 'pic', $this->pic])
//            ->andFilterWhere(['like', 'add_user', $this->add_user]);
        return $dataProvider;
    }
}
